package com.qf.controller.shangpin_controller;

import com.qf.bean.Employee;
import com.qf.dao.shangpin_dao.LoginDao;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 13/12/2019
 * Echo
 * sassspringboot
 * Alibaba
 */
@Controller
public class LoginController {

    @Autowired
    private LoginDao loginDao;
    @RequestMapping("/login")
    public String login(HttpServletRequest request,String username,String password) throws Exception{

        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            Employee login = loginDao.login(username);
            HttpSession session = request.getSession();
            session.setAttribute("haoge",login);
            return "index";
        } catch (AuthenticationException e) {
            return "login";
        }
    }

    @RequestMapping("/toLoginPage")
    public String toLoginPage(){
        return "login";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "login";
    }


}
